<?php

namespace Dongasai\WkProtoPhp\Enums;

/**
 * 流标志枚举
 * 使用PHP 8枚举语法
 */
enum StreamFlag: int
{
    case START = 0; // 开始
    case ING = 1;   // 进行中
    case END = 2;   // 结束

    /**
     * 获取流标志的字符串表示
     */
    public function toString(): string
    {
        return $this->name;
    }

    /**
     * 从值创建枚举实例
     */
    public static function fromValue(int $value): self
    {
        return self::from($value);
    }

    /**
     * 检查是否为开始标志
     */
    public function isStart(): bool
    {
        return $this === self::START;
    }

    /**
     * 检查是否为进行中标志
     */
    public function isInProgress(): bool
    {
        return $this === self::ING;
    }

    /**
     * 检查是否为结束标志
     */
    public function isEnd(): bool
    {
        return $this === self::END;
    }

    /**
     * 获取流标志描述
     */
    public function getDescription(): string
    {
        return match ($this) {
            self::START => '流开始',
            self::ING => '流进行中',
            self::END => '流结束',
        };
    }

    /**
     * 获取下一个流状态
     */
    public function getNext(): ?self
    {
        return match ($this) {
            self::START => self::ING,
            self::ING => self::END,
            self::END => null,
        };
    }

    /**
     * 获取前一个流状态
     */
    public function getPrevious(): ?self
    {
        return match ($this) {
            self::START => null,
            self::ING => self::START,
            self::END => self::ING,
        };
    }

    /**
     * 检查是否为有效的流状态转换
     */
    public function isValidTransition(self $next): bool
    {
        return match ($this) {
            self::START => $next === self::ING,
            self::ING => $next === self::END,
            self::END => false,
        };
    }

    /**
     * 获取所有流状态
     */
    public static function getAllStates(): array
    {
        return [
            self::START,
            self::ING,
            self::END,
        ];
    }

    /**
     * 检查是否为中间状态
     */
    public function isIntermediate(): bool
    {
        return $this === self::ING;
    }

    /**
     * 检查是否为边界状态
     */
    public function isBoundary(): bool
    {
        return in_array($this, [self::START, self::END]);
    }
}